package com.jhgk.jhcee.data.generate;


import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

/**
 * @Description:
 * @Author: lemon_xzp
 * @Date: 2021/9/15 21:29
 */
public class MySQLEntityCodeGenerator {
    public static void authGenerator() {
        final ResourceBundle rb = ResourceBundle.getBundle("mybatis-plus-generator");
        // 代码生成器
        AutoGenerator mpg = new AutoGenerator();
        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        gc.setOutputDir(projectPath + "/" + rb.getString("applicationName") + rb.getString("projectPath"));
        gc.setAuthor(rb.getString("author"));
        gc.setOpen(false);
        gc.setSwagger2(true); // 实体属性 Swagger2 注解
        gc.setBaseResultMap(true);
        gc.setServiceName("%sService");
//        gc.setFileOverride(false);
        gc.setDateType(DateType.ONLY_DATE);
        mpg.setGlobalConfig(gc);
        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl(rb.getString("url"));
        // dsc.setSchemaName("public");
        dsc.setDriverName(rb.getString("driverName"));
        dsc.setUsername(rb.getString("userName"));
        dsc.setPassword(rb.getString("password"));
        mpg.setDataSource(dsc);
        // 包配置
        PackageConfig pc = new PackageConfig();
        pc.setModuleName(rb.getString("moduleName"));
        pc.setParent(rb.getString("parent"));
        pc.setController("controller");
        pc.setService("service");
        pc.setServiceImpl("service.impl");
        pc.setEntity("entity");
        pc.setMapper("mapper");
        mpg.setPackageInfo(pc);
        // 自定义配置
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
                // to do nothing
            }
        };
        String templatePath = "/templates/mapper.xml.ftl";
        List<FileOutConfig> focList = new ArrayList<>();
        focList.add(new FileOutConfig(templatePath) {
            @Override
            public String outputFile(TableInfo tableInfo) {
                return projectPath + "/" + rb.getString("applicationName") + "/src/main/resources/mapper/" + pc.getModuleName()
                        + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
            }
        });
        cfg.setFileOutConfigList(focList);
        mpg.setCfg(cfg);
        // 配置模板
        TemplateConfig templateConfig = new TemplateConfig();
        // 配置自定义输出模板
        //指定自定义模板路径，注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别
        // templateConfig.setEntity("templates/entity2.java");
        // templateConfig.setService();
        // templateConfig.setController();
        if ("true".equals(rb.getString("entity"))) {
            String entityFtlPath = rb.getString("entity.ftl.path");
            if (StringUtils.isNotBlank(entityFtlPath)) {
                templateConfig.setEntity(entityFtlPath);
            }
        } else {
            templateConfig.setEntity(null);
        }

        if ("true".equals(rb.getString("mapper"))) {
            String mapperFtlPath = rb.getString("mapper.ftl.path");
            if (StringUtils.isNotBlank(mapperFtlPath)) {
                templateConfig.setMapper(mapperFtlPath);
            }
        } else {
            templateConfig.setMapper(null);
        }

        if ("true".equals(rb.getString("service"))) {
            String serviceFtlPath = rb.getString("service.ftl.path");
            if (StringUtils.isNotBlank(serviceFtlPath)) {
                templateConfig.setService(serviceFtlPath);
            }
        } else {
            templateConfig.setService(null);
        }

        if ("true".equals(rb.getString("serviceImp"))) {
            String serviceImpFtlPath = rb.getString("serviceImp.ftl.path");
            if (StringUtils.isNotBlank(serviceImpFtlPath)) {
                templateConfig.setServiceImpl(serviceImpFtlPath);
            }
        } else {
            templateConfig.setServiceImpl(null);
        }

        if ("true".equals(rb.getString("controller"))) {
            String controllerFtlPath = rb.getString("controller.ftl.path");
            if (StringUtils.isNotBlank(controllerFtlPath)) {
                templateConfig.setController(controllerFtlPath);
            }
        } else {
            templateConfig.setController(null);
        }
        templateConfig.setXml(null);
        mpg.setTemplate(templateConfig);
        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
//        strategy.setSuperEntityClass("你自己的父类实体,没有就不用设置!");
        strategy.setEntityLombokModel(true);
        strategy.setRestControllerStyle(true);
        // 公共父类
//        strategy.setSuperControllerClass("你自己的父类控制器,没有就不用设置!");
        // 写于父类中的公共字段
//        strategy.setSuperEntityColumns("id");
        strategy.setInclude(rb.getString("tableName").split(","));
        strategy.setControllerMappingHyphenStyle(true);
        strategy.setEntityTableFieldAnnotationEnable(true);
        String tablePrefix = rb.getString("tablePrefix");
        if (StringUtils.isNotBlank(tablePrefix)) {
            strategy.setTablePrefix(tablePrefix);
        }
        mpg.setStrategy(strategy);
        mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        mpg.execute();
    }

    public static void main(String[] args) {
        authGenerator();
    }
}
